<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>中国铁矿石含量分布地图</title>
  <!-- 引入外部资源 -->
  <script src="https://cdn.tailwindcss.com"></script>
  <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.8/dist/chart.umd.min.js"></script>
  <!-- 使用最新稳定版ECharts -->
  <script src="https://cdn.jsdelivr.net/npm/echarts@5.4.3/dist/echarts.min.js"></script>
  <!-- 引入中国地图数据 - 使用备用CDN链接 -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/echarts/5.4.3/map/js/china.js"></script>
  
  <!-- Tailwind 配置 -->
  <script>
    tailwind.config = {
      theme: {
        extend: {
          colors: {
            primary: '#165DFF',
            secondary: '#36CFC9',
            accent: '#FF7D00',
            dark: '#1D2129',
            light: '#F2F3F5',
            'iron-1': '#E6F7FF',
            'iron-2': '#BAE7FF',
            'iron-3': '#91D5FF',
            'iron-4': '#69C0FF',
            'iron-5': '#40A9FF',
            'iron-6': '#1890FF',
            'iron-7': '#096DD9',
          },
          fontFamily: {
            inter: ['Inter', 'system-ui', 'sans-serif'],
          },
        },
      }
    }
  </script>
  
  <!-- 自定义工具类 -->
  <style type="text/tailwindcss">
    @layer utilities {
      .content-auto {
        content-visibility: auto;
      }
      .map-container {
        height: 600px; /* 使用固定高度避免计算问题 */
        min-height: 400px;
      }
      .stat-card {
        @apply bg-white rounded-xl shadow-lg p-5 transition-all duration-300 hover:shadow-xl hover:-translate-y-1;
      }
    }
  </style>
</head>
<body class="font-inter bg-gray-50 text-dark">
  <!-- 页面标题区域 -->
  <header class="bg-white shadow-sm py-6 px-4 md:px-8">
    <div class="container mx-auto">
      <h1 class="text-[clamp(1.5rem,3vw,2.5rem)] font-bold text-dark flex items-center">
        <i class="fa fa-map-marker text-primary mr-3"></i>
        中国铁矿石含量分布地图
      </h1>
      <p class="text-gray-500 mt-2 max-w-3xl">展示中国各省市铁矿石储量及含量分布情况</p>
    </div>
  </header>

  <!-- 主要内容区域 -->
  <main class="container mx-auto px-4 md:px-8 py-6">
    <!-- 统计数据卡片 -->
    <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-8">
      <div class="stat-card">
        <div class="flex justify-between items-start">
          <div>
            <p class="text-gray-500 text-sm">全国总储量</p>
            <h3 class="text-2xl font-bold mt-1">214.6 亿吨</h3>
          </div>
          <div class="w-10 h-10 rounded-full bg-primary/10 flex items-center justify-center text-primary">
            <i class="fa fa-database"></i>
          </div>
        </div>
      </div>
      
      <div class="stat-card">
        <div class="flex justify-between items-start">
          <div>
            <p class="text-gray-500 text-sm">平均品位</p>
            <h3 class="text-2xl font-bold mt-1">32.8%</h3>
          </div>
          <div class="w-10 h-10 rounded-full bg-secondary/10 flex items-center justify-center text-secondary">
            <i class="fa fa-percent"></i>
          </div>
        </div>
      </div>
      
      <div class="stat-card">
        <div class="flex justify-between items-start">
          <div>
            <p class="text-gray-500 text-sm">主要产区</p>
            <h3 class="text-2xl font-bold mt-1">12 个省份</h3>
          </div>
          <div class="w-10 h-10 rounded-full bg-accent/10 flex items-center justify-center text-accent">
            <i class="fa fa-map"></i>
          </div>
        </div>
      </div>
      
      <div class="stat-card">
        <div class="flex justify-between items-start">
          <div>
            <p class="text-gray-500 text-sm">最高品位地区</p>
            <h3 class="text-2xl font-bold mt-1">河北省</h3>
          </div>
          <div class="w-10 h-10 rounded-full bg-iron-6/10 flex items-center justify-center text-iron-6">
            <i class="fa fa-trophy"></i>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 地图和控制面板 -->
    <div class="grid grid-cols-1 lg:grid-cols-4 gap-6">
      <!-- 左侧地图区域 -->
      <div class="lg:col-span-3">
        <div class="bg-white rounded-xl shadow-lg overflow-hidden">
          <div class="p-4 border-b flex justify-between items-center">
            <h2 class="font-semibold text-lg">铁矿石含量分布</h2>
            <div class="flex space-x-2">
              <button id="zoomIn" class="p-2 rounded hover:bg-gray-100 transition-colors">
                <i class="fa fa-search-plus text-gray-600"></i>
              </button>
              <button id="zoomOut" class="p-2 rounded hover:bg-gray-100 transition-colors">
                <i class="fa fa-search-minus text-gray-600"></i>
              </button>
              <button id="resetMap" class="p-2 rounded hover:bg-gray-100 transition-colors">
                <i class="fa fa-refresh text-gray-600"></i>
              </button>
            </div>
          </div>
          <!-- 地图容器 -->
          <div id="chinaMap" class="w-full map-container">
            <!-- 加载提示 -->
            <div id="mapLoading" class="w-full h-full flex items-center justify-center">
              <div class="text-center">
                <i class="fa fa-spinner fa-spin text-primary text-2xl mb-2"></i>
                <p class="text-gray-500">正在加载地图...</p>
              </div>
            </div>
            <!-- 错误提示 (默认隐藏) -->
            <div id="mapError" class="w-full h-full flex items-center justify-center hidden">
              <div class="text-center p-6">
                <i class="fa fa-exclamation-triangle text-orange-500 text-2xl mb-2"></i>
                <p class="text-gray-500 mb-2">地图加载失败</p>
                <button id="retryLoad" class="text-primary hover:underline">点击重试</button>
              </div>
            </div>
          </div>
          
          <!-- 图例 -->
          <div class="p-4 border-t bg-gray-50 flex flex-wrap items-center gap-x-6 gap-y-2">
            <span class="text-sm text-gray-600 mr-3">含量等级:</span>
            <div class="flex items-center">
              <div class="w-4 h-4 bg-iron-1 border border-gray-300 mr-2"></div>
              <span class="text-xs">极低 (&lt;20%)</span>
            </div>
            <div class="flex items-center">
              <div class="w-4 h-4 bg-iron-2 border border-gray-300 mr-2"></div>
              <span class="text-xs">低 (20-25%)</span>
            </div>
            <div class="flex items-center">
              <div class="w-4 h-4 bg-iron-3 border border-gray-300 mr-2"></div>
              <span class="text-xs">较低 (25-30%)</span>
            </div>
            <div class="flex items-center">
              <div class="w-4 h-4 bg-iron-4 border border-gray-300 mr-2"></div>
              <span class="text-xs">中等 (30-35%)</span>
            </div>
            <div class="flex items-center">
              <div class="w-4 h-4 bg-iron-5 border border-gray-300 mr-2"></div>
              <span class="text-xs">较高 (35-40%)</span>
            </div>
            <div class="flex items-center">
              <div class="w-4 h-4 bg-iron-6 border border-gray-300 mr-2"></div>
              <span class="text-xs">高 (40-45%)</span>
            </div>
            <div class="flex items-center">
              <div class="w-4 h-4 bg-iron-7 border border-gray-300 mr-2"></div>
              <span class="text-xs">极高 (&gt;45%)</span>
            </div>
          </div>
        </div>
      </div>
      
      <!-- 右侧数据面板 -->
      <div class="lg:col-span-1 flex flex-col gap-6">
        <!-- 地区详情 -->
        <div class="bg-white rounded-xl shadow-lg p-5">
          <h2 class="font-semibold text-lg mb-4">地区详情</h2>
          <div id="regionDetail" class="space-y-4">
            <div class="text-center text-gray-500 py-8">
              <i class="fa fa-hand-pointer-o text-2xl mb-2"></i>
              <p>请在地图上点击省份查看详情</p>
            </div>
          </div>
        </div>
        
        <!-- 主要产区分布 -->
        <div class="bg-white rounded-xl shadow-lg p-5">
          <h2 class="font-semibold text-lg mb-4">主要产区占比</h2>
          <div class="h-64">
            <canvas id="productionChart"></canvas>
          </div>
        </div>
      </div>
    </div>
  </main>
  
  <!-- 页脚 -->
  <footer class="bg-dark text-white py-6 mt-10">
    <div class="container mx-auto px-4 md:px-8 text-center text-sm text-gray-400">
      <p>© 2025 中国铁矿石资源分布数据可视化平台</p>
    </div>
  </footer>

  <script>
    // 等待DOM加载完成
    document.addEventListener('DOMContentLoaded', function() {
      // 检查ECharts是否已加载
      if (typeof echarts === 'undefined') {
        console.error('ECharts未加载');
        const mapLoading = document.getElementById('mapLoading');
        const mapError = document.getElementById('mapError');
        if (mapLoading) mapLoading.classList.add('hidden');
        if (mapError) mapError.classList.remove('hidden');
        return;
      }
      
      // 延迟初始化地图，确保所有资源加载完成
      setTimeout(function() {
        initMapWithCheck();
      }, 100);
    });
    
    // 检查依赖并初始化地图
    function initMapWithCheck() {
      // 确保地图数据已加载
      if (!echarts.getMap('china')) {
        console.error('中国地图数据未加载');
        const mapLoading = document.getElementById('mapLoading');
        const mapError = document.getElementById('mapError');
        if (mapLoading) mapLoading.classList.add('hidden');
        if (mapError) mapError.classList.remove('hidden');
        // 如果地图数据未加载，尝试重新初始化
        setTimeout(function() {
          if (echarts.getMap('china')) {
            initMap();
          } else {
            console.error('中国地图数据仍未加载');
          }
        }, 1000);
        return;
      }
      
      // 初始化地图
      initMap();
    }
    
    // 铁矿石含量数据
    const ironOreData = [
      {name: '北京', value: 22.5},
      {name: '天津', value: 24.8},
      {name: '河北', value: 38.2},
      {name: '山西', value: 31.5},
      {name: '内蒙古', value: 33.7},
      {name: '辽宁', value: 35.9},
      {name: '吉林', value: 27.3},
      {name: '黑龙江', value: 29.1},
      {name: '上海', value: 18.7},
      {name: '江苏', value: 26.4},
      {name: '浙江', value: 21.2},
      {name: '安徽', value: 30.5},
      {name: '福建', value: 23.6},
      {name: '江西', value: 28.8},
      {name: '山东', value: 32.3},
      {name: '河南', value: 29.7},
      {name: '湖北', value: 34.2},
      {name: '湖南', value: 25.9},
      {name: '广东', value: 22.8},
      {name: '广西', value: 26.7},
      {name: '海南', value: 20.3},
      {name: '重庆', value: 31.1},
      {name: '四川', value: 36.5},
      {name: '贵州', value: 24.3},
      {name: '云南', value: 30.9},
      {name: '西藏', value: 19.5},
      {name: '陕西', value: 28.2},
      {name: '甘肃', value: 27.6},
      {name: '青海', value: 23.1},
      {name: '宁夏', value: 21.7},
      {name: '新疆', value: 32.8}
    ];
    
    // 储量数据（单位：亿吨）
    const reserveData = {
      '河北': 62.3,
      '辽宁': 45.8,
      '四川': 32.6,
      '山西': 21.5,
      '内蒙古': 18.7,
      '安徽': 9.2,
      '山东': 8.4,
      '湖北': 7.5,
      '云南': 5.2,
      '其他': 23.4
    };
    
    // 初始化地图函数
    function initMap() {
      try {
        // 显示加载状态
        const mapLoading = document.getElementById('mapLoading');
        const mapError = document.getElementById('mapError');
        
        if (mapLoading) {
          mapLoading.classList.remove('hidden');
        }
        if (mapError) {
          mapError.classList.add('hidden');
        }
        
        // 初始化地图实例
        const mapContainer = document.getElementById('chinaMap');
        if (!mapContainer) {
          throw new Error('地图容器未找到');
        }
        
        const mapChart = echarts.init(mapContainer);
        
        // 地图配置
        const option = {
          tooltip: {
            trigger: 'item',
            formatter: function(params) {
              if (!params.name || params.value === undefined) return '';
              return `${params.name}<br>铁矿石平均品位: ${params.value}%`;
            }
          },
          visualMap: {
            type: 'continuous',
            min: 15,
            max: 45,
            range: [15, 45],
            calculable: true,
            inRange: {
              color: ['#E6F7FF', '#BAE7FF', '#91D5FF', '#69C0FF', '#40A9FF', '#1890FF', '#096DD9']
            },
            show: false
          },
          series: [
            {
              name: '铁矿石含量',
              type: 'map',
              map: 'china',
              roam: true,
              label: {
                show: true,
                fontSize: 12,
                color: '#333'
              },
              data: ironOreData,
              emphasis: {
                label: {
                  show: true,
                  color: '#333'
                },
                itemStyle: {
                  shadowBlur: 10,
                  shadowColor: 'rgba(0, 0, 0, 0.3)'
                }
              }
            }
          ]
        };
        
        // 设置地图配置
        mapChart.setOption(option, true);
        
        // 隐藏加载状态
        setTimeout(() => {
          if (mapLoading) {
            mapLoading.classList.add('hidden');
          }
        }, 500);
        
        // 地图点击事件
        mapChart.on('click', function(params) {
          if (params.name && params.value !== undefined) {
            showRegionDetail(params.name, params.value);
          }
        });
        
        // 地图控制按钮
        const zoomInBtn = document.getElementById('zoomIn');
        const zoomOutBtn = document.getElementById('zoomOut');
        const resetMapBtn = document.getElementById('resetMap');
        
        if (zoomInBtn) {
          zoomInBtn.addEventListener('click', function() {
            mapChart.dispatchAction({ type: 'zoom', zoom: 1.1 });
          });
        }
        
        if (zoomOutBtn) {
          zoomOutBtn.addEventListener('click', function() {
            mapChart.dispatchAction({ type: 'zoom', zoom: 0.9 });
          });
        }
        
        if (resetMapBtn) {
          resetMapBtn.addEventListener('click', function() {
            mapChart.dispatchAction({ type: 'restore' });
          });
        }
        
        // 响应窗口大小变化
        window.addEventListener('resize', function() {
          if (mapChart) {
            mapChart.resize();
          }
        });
        
        return mapChart;
        
      } catch (error) {
        console.error('地图初始化失败:', error);
        // 显示错误状态
        const mapLoading = document.getElementById('mapLoading');
        const mapError = document.getElementById('mapError');
        
        if (mapLoading) {
          mapLoading.classList.add('hidden');
        }
        if (mapError) {
          mapError.classList.remove('hidden');
        }
        return null;
      }
    }
    
    // 重试加载地图
    document.addEventListener('click', function(event) {
      if (event.target.id === 'retryLoad') {
        initMapWithCheck();
      }
    });
    
    // 显示地区详情
    function showRegionDetail(region, value) {
      const regionDetail = document.getElementById('regionDetail');
      if (!regionDetail) return;
      
      let reserve = reserveData[region] || (Math.random() * 5 + 0.5).toFixed(1);
      let rank = getRankByValue(value);
      
      regionDetail.innerHTML = `
        <div class="text-center mb-4">
          <h3 class="text-xl font-bold">${region}</h3>
          <div class="w-16 h-1 bg-primary mx-auto mt-2"></div>
        </div>
        
        <div class="space-y-3">
          <div class="flex justify-between items-center p-2 hover:bg-gray-50 rounded">
            <span class="text-gray-600">平均品位</span>
            <span class="font-semibold ${getColorClassByValue(value)}">${value}%</span>
          </div>
          
          <div class="flex justify-between items-center p-2 hover:bg-gray-50 rounded">
            <span class="text-gray-600">预估储量</span>
            <span class="font-semibold">${reserve} 亿吨</span>
          </div>
          
          <div class="flex justify-between items-center p-2 hover:bg-gray-50 rounded">
            <span class="text-gray-600">全国排名</span>
            <span class="font-semibold">第 ${rank} 位</span>
          </div>
          
          <div class="flex justify-between items-center p-2 hover:bg-gray-50 rounded">
            <span class="text-gray-600">资源评级</span>
            <span class="font-semibold ${getRatingClass(value)}">${getRatingText(value)}</span>
          </div>
        </div>
      `;
    }
    
    // 辅助函数 - 获取排名
    function getRankByValue(value) {
      const sortedValues = [...ironOreData].sort((a, b) => b.value - a.value);
      const index = sortedValues.findIndex(item => item.value === value);
      return index !== -1 ? index + 1 : Math.floor(Math.random() * 10) + 1;
    }
    
    // 辅助函数 - 获取颜色类
    function getColorClassByValue(value) {
      if (value < 20) return 'text-iron-1';
      if (value < 25) return 'text-iron-2';
      if (value < 30) return 'text-iron-3';
      if (value < 35) return 'text-iron-4';
      if (value < 40) return 'text-iron-5';
      if (value < 45) return 'text-iron-6';
      return 'text-iron-7';
    }
    
    // 辅助函数 - 获取评级文本
    function getRatingText(value) {
      if (value < 20) return '极低级';
      if (value < 25) return '低级';
      if (value < 30) return '较低级';
      if (value < 35) return '中等级';
      if (value < 40) return '较高级';
      if (value < 45) return '高级';
      return '极高级';
    }
    
    // 辅助函数 - 获取评级类
    function getRatingClass(value) {
      if (value < 30) return 'text-orange-500';
      if (value < 40) return 'text-blue-500';
      return 'text-green-500';
    }
    
    // 初始化生产占比图表
    document.addEventListener('DOMContentLoaded', function() {
      // 延迟初始化图表，确保Canvas上下文可用
      setTimeout(function() {
        const productionChartCtx = document.getElementById('productionChart');
        if (productionChartCtx) {
          const productionCtx = productionChartCtx.getContext('2d');
          if (productionCtx) {
            const productionChart = new Chart(productionCtx, {
              type: 'doughnut',
              data: {
                labels: Object.keys(reserveData),
                datasets: [{
                  data: Object.values(reserveData),
                  backgroundColor: [
                    '#1890FF', '#40A9FF', '#69C0FF', '#91D5FF', '#BAE7FF',
                    '#36CFC9', '#52C41A', '#FAAD14', '#F5222D', '#722ED1'
                  ],
                  borderWidth: 0,
                  hoverOffset: 4
                }]
              },
              options: {
                responsive: true,
                maintainAspectRatio: false,
                plugins: {
                  legend: {
                    position: 'bottom',
                    labels: {
                      boxWidth: 12,
                      padding: 15,
                      font: { size: 11 }
                    }
                  }
                },
                cutout: '65%'
              }
            });
          }
        }
      }, 200);
    });
  </script>
</body>
</html>